# *** Function definitions for program readmeter.py ***
""" ****************************************************************
   plotcfg.py, ver 0.9.2; Phase II
   Rev - Last: 03/12/2013 - Previous: 03/11/2013 (AEM)

   Support functions for PlotDMM

 This file is part of the DMM Reader project.

 Copyright (C) 2012-2013  Arnold E. Morgan

    DMM Reader is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

"""

import sys
import os
import datetime

CfgFileName = "readmeter.cfg"

# ********** Function Definitions **********

def GetCfgSettings(ParentPath):
   """ Read in serial port config settings.
       Config file MUST be named 'readmeter.cfg'
       and located in the DMM Reader install directory.

       Return values as a two value tuple;
       1st value: rtn state: 0 = fail, 1 = success
       2nd value: cfg data as a dictionary
   """

   CfgPath = os.path.join(ParentPath, CfgFileName)
   RtnValue = (False, '')
   ConfigValues = {}
   print "Ready to open and read config file..."
   try:
      for line in open(CfgPath, 'r'):
         TempStr = line.strip()
         if len(TempStr) > 0 and TempStr[0] != "#" and TempStr.find('=') != -1:
            (theKey, theValue) = TempStr.split("=")
            ConfigValues[theKey.strip()] = theValue.strip()      

      RtnValue = (True, ConfigValues)
      
   except IOError, (errno, strerror):
      print "\nCfg file open error(%s): %s\n" % (errno, strerror)
   except:
      #print "Unexpected error: ", sys.exc_info()[0]
      print "\nUnexpected cfg file read error: %s\n" % sys.exc_info()
   finally: 
      print "Config file settings read complete"

   return RtnValue
# *************** End of fcn GetCfgSettings ************

# **********************************************
# *** DESIGN NOTE: Add fcn: CheckInitValues(...) to
# ***              - validate integer values
# ***              - Confirm all values supplied
# **********************************************


